import 'package:flutter/material.dart';
import 'ScaleAnimationRoute.dart';
import 'ChangeRouteAnimationRoute.dart';
import 'hero_animation.dart';
import 'StaggerAnimationRoute.dart';
import 'animation_structure.dart';
import 'hero_animation2.dart';

class AnimationsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _AnimationsPageState();
  }
}

class _AnimationsPageState extends State<AnimationsPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Wrap(
          spacing: 8.0,
          runSpacing: 4.0,
          alignment: WrapAlignment.center,
          children: <Widget>[
            RaisedButton(
              colorBrightness: Brightness.dark,
              child: Text('动画结构'),
              onPressed: () {
                Navigator.push(context,
                    new MaterialPageRoute(builder: (context) {
                  return new AnimationStructure();
                }));
              },
            ),
            RaisedButton(
              colorBrightness: Brightness.dark,
              child: Text('缩放动画'),
              onPressed: () {
                Navigator.push(context,
                    new MaterialPageRoute(builder: (context) {
                  return new ScaleAnimationRoute();
                }));
              },
            ),
            RaisedButton(
              colorBrightness: Brightness.dark,
              child: Text('自定义路由切换动画'),
              onPressed: () {
                /* Navigator.push(context,
                    new MaterialPageRoute(builder: (context) {
                      return new ChangeRouteAnimationRoute();
                    }));*/
                /*Navigator.push(
                    context,
                    PageRouteBuilder(
                        transitionDuration:
                            Duration(milliseconds: 500), //动画时间为500毫秒
                        pageBuilder: (BuildContext context, Animation animation,
                            Animation secondaryAnimation) {
                          return new FadeTransition(
                            //使用渐隐渐入过渡,
                            opacity: animation,
                            child: ChangeRouteAnimationRoute(), //路由B
                          );
                        }));*/
                Navigator.push(
                  context,
                  FadeRoute(
                    builder: (context) {
                      return ChangeRouteAnimationRoute();
                    },
                    transitionDuration: const Duration(milliseconds: 500),
                  ),
                );
              },
            ),
            RaisedButton(
              colorBrightness: Brightness.dark,
              child: Text('Hero动画'),
              onPressed: () {
                Navigator.push(context,
                    new MaterialPageRoute(builder: (context) {
                  return new HeroAnimationRoute();
                }));
              },
            ),
            RaisedButton(
              colorBrightness: Brightness.dark,
              child: Text('Hero动画2'),
              onPressed: () {
                Navigator.push(context,
                    new MaterialPageRoute(builder: (context) {
                  return new HeroAnimation2();
                }));
              },
            ),
            RaisedButton(
              colorBrightness: Brightness.dark,
              child: Text('交错动画'),
              onPressed: () {
                Navigator.push(context,
                    new MaterialPageRoute(builder: (context) {
                  return new StaggerAnimationRoute();
                }));
              },
            ),
          ],
        ),
      ),
    );
  }
}

//继承PageRoute实现一个动画切换的路由类
class FadeRoute extends PageRoute {
  FadeRoute({
    @required this.builder,
    this.transitionDuration = const Duration(milliseconds: 300),
    this.opaque = true,
    this.barrierDismissible = false,
    this.barrierColor,
    this.barrierLabel,
    this.maintainState = true,
  });

  final WidgetBuilder builder;

  @override
  final Duration transitionDuration;

  @override
  final bool opaque;

  @override
  final bool barrierDismissible;

  @override
  final Color barrierColor;

  @override
  final String barrierLabel;

  @override
  final bool maintainState;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation) =>
      builder(context);

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    return FadeTransition(
      opacity: animation,
      child: builder(context),
    );
  }
}
